
/// <reference path="base/framwork.js" />
/// <reference path="widget/iscroll.js" />
/// <reference path="base/jquery.js" />
/// <reference path="base/transit.js" />

$(document).ready(function () {
    var loadBox = $('aside.loadBox');
    var articleBox = $('article');
    icom.orient(init);

    function init() {
        //loadBox.show();
        //iuser.init(userGetted);
        load_handler();
    }

    function userGetted(data) {
        console.log('用户头像：' + data.headimage);
        console.log('用户昵称：' + data.nickname);
        load_handler();
    }

    function load_handler() {
        var loader = new PxLoader();
        loader.addImage('images/bg.jpg');
        loader.addImage('images/btn-depict.png');
        loader.addImage('images/btn-deserts.png');
        loader.addImage('images/logo.png');
        loader.addImage('images/templete.png');
        loader.addImage('images/1.png');
        loader.addImage('images/2.png');
        loader.addImage('images/3.png');
        //实际加载进度
        loader.addProgressListener(function (e) {
            var per = Math.round(e.completedCount / e.totalCount * 50);
            //loadPer.html(per + '%');
        });
        loader.addCompletionListener(function () {
            init_handler();
            loader = null;
        });
        loader.start();
    }


    function init_handler() {
        console.log('init handler');
        monitor_handler();
        idx.init();
        idx.orderinit();
        event_handler();
    }

    //----------------------------------------页面监测代码----------------------------------------
    function monitor_handler() {
        //imonitor.add({obj:$('a.btnTest'),action:'touchstart',category:'首页',label:'测试按钮'});
    }

    function event_handler() {
        // 同善基金
        $("#btn-allpay").on("click", function () { location = "merits.html" });
        // 一日一善
        $("#btn-daypay").on("click", function () { location = "day.html" });
        // 法务登记
        $("#btn-info").on("click", function () { location = "reg.html" });
    }

});

var idx = indexModel();
function indexModel() {
    var i = {},
        toptime;

    i.init = function () {
        //kv init
        i.kvinit();
        $("footer").load("/module/foot.html");
    }

    i.kvinit = function () {
        var kvs = $('.swiper-wrapper');
        $.get('webajax/getnews.ashx', function (data) {
            var news = JSON.parse(data);
            kvs.children().remove();
            $(news).each(function () {
                kvs.append('<li class="swiper-slide"><a href="' + this.Attachment + '"><img src="upload/news/' + this.Photo + '" /><p>' + this.Title + '</p></a></li>');
            });
            i.kv();
        });
    }

    i.orderinit = function () {
        var obj = $('#top > ul');
        $.get('webajax/getnamelist.ashx', function (res) {
            if (res.length > 0) {
                $.each(res, function (i, item) {
                    obj.append('<li><p>' + item.nickname + '</p><p>' + item.tyname + '</p><p>' + item.amount + '</p></li>');
                });
                i.top();
            }
        }, 'json');
    }

    i.kv = function () {
        var ul = $("#kv > ul");
        ul.width(ul.children("li").length * ul.children("li").width());
        var mySwiper = new Swiper('#kv', {
            speed: 500,
            loop: true
        });
    }

    i.top = function () {
        var ul = $("#top > ul"),
            li = ul.children("li"),
            num = 0;
        ul.height(li.length * li.height());
        toptime = setInterval(function () {
            var tmp = li.eq(num);
            ul.transition({ y: "-=" + (li.height() * 1.75) }, 260, function () {
                num++;
                if (num + 6 == li.length) {
                    ul.transit({ y: 0 }, 10);
                    num = 0;
                }
            });
        }, 2000);
    }
    return i;
}
